package org.sohnemann;

import java.io.*;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class ConfigFileParser {
	
	public ConfigFileParser(String fileName)
	{
		try {
			in = new DataInputStream(new FileInputStream(fileName));
		} catch (FileNotFoundException e) {
			System.exit(0);
		}
		p = Pattern.compile(".*?\"([^\"]+)\".*?\"([^\"]+)\".*?\\.");
		cfg = new HashMap<String, String>();
	}
	
	public void parseConfig()
	{
		String entireCfg = "";
		try {
			while(in.available() != 0) {
				entireCfg += in.readLine();
			}
		} catch (IOException e) {
			System.exit(0);
		}
		m = p.matcher(entireCfg);
		
		while(m.find()) {
			for(int i=0, j=m.groupCount(); i<=j; i++) {
				if(i == 2 || i%3 == 0 || i==0) continue;
				cfg.put(m.group(i), m.group(i+1));
			}
		}
	}
	
	public String get(String key)
	{
		return cfg.get(key);
	}
	
	private DataInputStream in;
	private Pattern p;
	private Matcher m;
	private HashMap<String, String> cfg;
}
